/*
	The Raja Toolkit.
	Copyright (C) 2011 Raja-Toolkit Foundation

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 3.0 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "../sampler2.hpp"

#include "gles2_internal.hpp"

#include <FreeImage.h>

using namespace std;

namespace raja
{
namespace render
{

struct internal_format
{
	internal_format() : int_fmt(0), img_fmt(0), dtype(0)
	{
	}

	internal_format(GLint int_fmt_, GLenum img_fmt_, GLenum dtype_) : int_fmt(int_fmt_), img_fmt(img_fmt_), dtype(dtype_)
	{
	}

	GLint		int_fmt;	///< internal data format
	GLenum		img_fmt;	///< image format
	GLenum		dtype;		///< data type
};

/// map sampler format to internal format
/// @todo change this to unordered list
map<SAMPLER_FORMAT, internal_format>		sf_to_if;

/// fill in sampler format to internal format conversion map
static bool fill_sf_to_if()
{
	sf_to_if[SF_NOTYPE_A8]			= internal_format(GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE);
	sf_to_if[SF_NOTYPE_R8G8B8]		= internal_format(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE);
	sf_to_if[SF_NOTYPE_R8G8B8A8]	= internal_format(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE);

	sf_to_if[SF_UINT_A8]			= internal_format(GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE);
	sf_to_if[SF_UINT_R8G8B8]		= internal_format(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
	sf_to_if[SF_UINT_R8G8B8A8]		= internal_format(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);

	return true;
}

static bool __init_formats	= fill_sf_to_if();

sampler2* sampler2::create(uint32 width, uint32 height, SAMPLER_FORMAT st, RESOURCE_USAGE ru, const void *data)
{
	GLuint tex_id;

	internal_format	fmt	= sf_to_if[st];

	glGenTextures(1, &tex_id);
	glBindTexture(GL_TEXTURE_2D, tex_id);
	glTexImage2D(GL_TEXTURE_2D, 0, fmt.int_fmt, width, height, 0, fmt.img_fmt, fmt.dtype, data);

	if( glGetError() != GL_NO_ERROR )
	{
		glDeleteTextures(1, &tex_id);
		cout << "error setting up the texture" << endl;
		return NULL;
	}

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	sampler2	*smpl	= new sampler2();
	smpl->__width	= width;
	smpl->__height	= height;
	smpl->__tex_id	= tex_id;

	return smpl;
}

sampler2::~sampler2()
{
	glDeleteTextures(1, &__tex_id);
}

} // namespace render
} // namespace raja


